var baseURL = 'https://trial.beelink.com:10025';
var authorities = "";
var type_to = "index";
var signUpButton = document.getElementById('signUp');
var signInButton = document.getElementById('signIn');
var container = document.getElementById('dowebok');
var $_GET = (function () {
    var url = window.document.location.href.toString();
    var u = url.split("?");
    if (typeof (u[1]) == "string") {
        u = u[1].split("&");
        var get = {};
        for (var i in u) {
            var j = u[i].split("=");
            get[j[0]] = j[1];
        }
        return get;
    } else {
        return {};
    }
})();
signUpButton.addEventListener('click', function () {
    container.classList.add('right-panel-active')
});
signInButton.addEventListener('click', function () {
    container.classList.remove('right-panel-active')
});

$(function () {
    var login_vcode = true;
    var login_type = $_GET["type"] ? $_GET["type"] : "login";
    switch (login_type) {
        case "login":
            container.classList.remove('right-panel-active')
            break;
        case "logon":
            container.classList.add('right-panel-active')
            break;
    }

    $("#teb").click(function () {
        if (login_vcode == true) {
            $("#login_password").show();
            $("#vcode_show").hide();
            $(this).text("验证码登录");
            login_vcode = false
        } else {
            $("#login_password").hide();
            $("#vcode_show").show();
            $(this).text("密码登录");
            login_vcode = true
        }
    })

    //登录
    $("#login_sub").on("click", function () {
        let userName = $("#login_phone").val()
        let password = $("#login_password").val()
        let phoneCode = $("#yanzhengma_text2").val()
        if (login_vcode == false) {
            console.log(1)
            $.ajax({
                url: baseURL + '/login/api/web/login',
                type: 'post',
                dataType: 'json',
                async: false,//同步
                // headers: {'authorities': authorities},
                data: {
                    "userName": userName,
                    "password": password
                },
                success: function (e) {
                    console.log(e)
                    if (e.code == 1) {
                        e.data[0].password = password
                        let authorities = e.msg
                        let user_data = JSON.stringify(e.data[0])
                        let user_menu = JSON.stringify(e.data[1])
                        // let user_app = JSON.stringify(e.data[2])
                        // $.session.set('authorities', authorities)
                        // $.session.set('user_data', user_data)
                        // $.session.set('user_menu', user_menu)

                        $.cookie('authorities', authorities, {expires: 7, path: '/'})
                        $.cookie('user_data', user_data, {expires: 7, path: '/'});
                        type_to = $_GET["type_to"] ? $_GET["type_to"] : "index";
                        window.location.href = type_to + ".html";
                    } else {
                        alert(e.msg)
                    }
                }
            })
        } else {
            console.log(2)
            $.ajax({
                url: baseURL + '/login/api/web/registerPhoneCode',
                type: 'post',
                dataType: 'json',
                async: false,//同步
                // headers: {'authorities': authorities},
                data: {
                    "phone": userName,
                    "code": phoneCode
                },
                success: function (e) {
                    console.log(e)
                    if (e.code == 1) {
                        e.data[0].password = password
                        let authorities = e.msg
                        let user_data = JSON.stringify(e.data[0])
                        let user_menu = JSON.stringify(e.data[1])
                        // let user_app = JSON.stringify(e.data[2])
                        // $.session.set('authorities', authorities)
                        // $.session.set('user_data', user_data)
                        // $.session.set('user_menu', user_menu)

                        $.cookie('authorities', authorities, {expires: 7, path: '/'})
                        $.cookie('user_data', user_data, {expires: 7, path: '/'});
                        type_to = $_GET["type_to"] ? $_GET["type_to"] : "index";
                        window.location.href = type_to + ".html";
                    } else {
                        alert(e.msg)
                    }
                }
            })
        }


    })

    //注册
    $("#register_sub").on("click", function () {
        let phone = $("#logon_phone").val()
        let phoneCode = $("#yanzhengma_text1").val()
        let password = $("#login_password").val()
        $.ajax({
            url: baseURL + '/login/api/web/register',
            type: 'post',
            dataType: 'json',
            // async: false,//同步
            headers: {'authorities': authorities},
            data: {
                "userName": phone,
                "password": password,
                "phoneCode": phoneCode,
            },
            success: function (e) {
                if (e.code == -1) {
                    alert("此账号已经注册")
                } else {
                    alert(e.msg)
                }
            }
        })

    })

    //获取验证码
    function test(type) {
        var phone
        if (type == 1) {
            phone = $("#logon_phone").val()
        } else {
            phone = $("#login_phone").val()
        }

        QVerify({
            // name: "人机验证",  // 验证面板标题
            desc: "滑动滑块，使图片显示角度为正",  // 验证操作提示文字
            cloneTxt: "点我关闭",  // 关闭验证面板文字
            successTxt: "验证成功，{0}秒后自动关闭",  // {0}必须有，延迟关闭时间显示
            errorTxt: "验证失败，请重试",
            images: [
                "style/QVerify/img/t1.png",
                "style/QVerify/img/t2.png",
                "style/QVerify/img/t3.png",
                "style/QVerify/img/t4.png",
                "style/QVerify/img/t5.png",
            ],  // 图片数组 将会随机从里面选取一张
            duration: 2,  //定时关闭时间 默认 2
            mountDiv: "#QVerify",  // 装载div 默认 #QVerify
            slideDifference: 25,  // 滑动误差值 默认 5
            defaultDifference: 50,  // 默认图片角度最小差值 默认 50
            mousedown: function (e) {  // 按下鼠标事件
                // e: 元素本身
                // console.log('按下了鼠标');
            },
            mousemove: function (e, moveWidth) {  // 移动鼠标事件
                // e: 元素本身
                // moveWidth: 移动距离
                // console.log("移动了鼠标");
                // console.log(moveWidth);
            },
            mouseup: function (e, moveWidth) {  // 抬起鼠标事件
                // e: 元素本身
                // moveWidth: 移动距离
                // console.log("抬起了鼠标");
                // console.log(moveWidth);
            },
            success: function () {  // 验证成功事件
                console.log("验证成功");
                if (timeClock == 30) {
                    $.ajax({
                        url: baseURL + '/login/api/web/getPhoneCode',
                        type: 'post',
                        dataType: 'json',
                        // async: false,//同步
                        headers: {'authorities': authorities},
                        data: {
                            "phone": phone,
                        },
                        success: function (e) {
                            console.log(e)
                            if (e.code == 1) {
                                if (type == 1) {
                                    sendCode()
                                } else {
                                    sendCode2()
                                }
                            } else {
                                alert(e.msg)
                            }
                        }
                    })
                }
            },
            fail: function () {  // 验证失败事件
                console.log("验证失败");
            },
            complete: function () {  // 验证完成事件 成功失败都会执行(执行顺序: complete->success 或 complete->fail)
                // console.log("触发验证");
            },
            clone: function (status) {  // 关闭验证面板事件
                // status 返回的状态
                // false: 失败状态下关闭; true: 成功状态下关闭;
                console.log(status);
            }
        });
    };

    $("#yanzhengma_but1").on("click", function () {
        if (timeClock == 30) {
            test(1);
        }
    })
    $("#yanzhengma_but2").on("click", function () {
        if (timeClock == 30) {
            test(2);
        }
    })

    //秒倒计时
    var timeClock = 30

    function sendCode() {
        var timer_num = 30;
        $("#yanzhengma_but1").disabled = true;
        timeClock = setInterval(function () {
            timer_num--;
            $('#yanzhengma_but1').val(timer_num + " 秒");

            if (timer_num == 0) {
                clearInterval(timeClock);
                timeClock = 30
                $('#yanzhengma_but1').val("获取验证码");
                $("#yanzhengma_but1").disabled = false;
            }
        }, 1000)
    }

    function sendCode2() {
        var timer_num = 30;
        $("#yanzhengma_but2").disabled = true;
        timeClock = setInterval(function () {
            timer_num--;
            $('#yanzhengma_but2').val(timer_num + " 秒");

            if (timer_num == 0) {
                clearInterval(timeClock);
                timeClock = 30
                $('#yanzhengma_but2').val("获取验证码");
                $("#yanzhengma_but2").disabled = false;
            }
        }, 1000)
    }


})
